<?php
class Master_pegawai extends CI_Model 
{

	function __construct()
	{
		parent::__construct();
	}
	
	function add($dataInsert)
	{
		$query = $this->db->insert('mr_pegawai', $dataInsert);
		return $query;
	}
		
	function read()
	{	
		$this->db->where("id_mr_pegawai <> '0'");
		$this->db->order_by('nm_pegawai');		
		$query=$this->db->get('mr_pegawai');
		return $query->result();
	}
	
	function edit($id_mr_pegawai)
	{
		$this->db->where('id_mr_pegawai',$id_mr_pegawai);
		$query=$this->db->get('mr_pegawai');
		return $query->result();
	}
	
	function update($id_mr_pegawai,$dataUpdate)
	{
		$this->db->where('id_mr_pegawai', $id_mr_pegawai);
		$update = $this->db->update('mr_pegawai', $dataUpdate);
		return $update;
	}
	
	function detail($id_mr_pegawai)
	{
		$this->db->select('mr_pegawai.*,mr_pendidikan.*,mr_agama.*');
		$this->db->from('mr_pegawai');
		$this->db->join('mr_pendidikan','mr_pendidikan.id_mr_pendidikan = mr_pegawai.ijazah_akhir','left');
		$this->db->join('mr_agama','mr_agama.id_mr_agama = mr_pegawai.id_mr_agama','left');
		$this->db->where('id_mr_pegawai',$id_mr_pegawai);
		
		$query = $this->db->get();		
		return $query->result();		
	}
	
	function delete($id_mr_pegawai)
	{
		$this->db->where('id_mr_pegawai', $id_mr_pegawai);
		$delete = $this->db->delete('mr_pegawai');
		return $delete;
	}

	function limit($limit,$offset)
	{	
		$this->db->where("id_mr_pegawai <> '0'");
		$this->db->order_by('nm_pegawai');		
		$query=$this->db->get('mr_pegawai',$limit,$offset);
		return $query->result();
	}

	function num_rows()
	{	
		$this->db->where("id_mr_pegawai <> '0'");
		$query = $this->db->get('mr_pegawai');
		return $query->num_rows();
	}

	function nip()
	{	
		$sql="select max(right(nip,3)) as nip from mr_pegawai";
		$query=$this->db->query($sql);
		return $query->row_array();
	}

	function limit_search($keyword,$limit,$offset){
		$sql="select * from mr_pegawai where (nip like '%".$keyword."%' or nm_pegawai like '%".$keyword."%')
			limit $offset,$limit";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result();
		}else{
			return array();
		}
	}

	function num_rows_search($keyword){	
		
		$sql="select * from mr_pegawai where (nip like '%".$keyword."%' or nm_pegawai like '%".$keyword."%')";
		//echo $sql;
		$query=$this->db->query($sql);
		return $query->num_rows();
	}
	
}